package puzzle.masyu2.view.text;

import java.io.IOException;

import puzzle.common.point.Direction;
import puzzle.masyu2.data.MasyuBoard;

public class MasyuTextFormatter {
	private Appendable m_out;

	public MasyuTextFormatter(Appendable out) {
		m_out = out;
	}

	public void format(MasyuBoard b) throws IOException {
		for (int y = 0; y <= b.height; y++) {
			for (int x = 0; x < b.width; x++) {
				m_out.append("┼");
				if (b.getForbiddenFlag(x, y, Direction.N)) {
					assert b.getForbiddenFlag(x, y - 1, Direction.S);
					m_out.append("×");
				} else if (b.getExistFlag(x, y, Direction.N)) {
					assert b.getExistFlag(x, y - 1, Direction.S);
					m_out.append("╂");
				} else {
					assert !b.getForbiddenFlag(x, y - 1, Direction.S)
							&& !b.getExistFlag(x, y - 1, Direction.S);
					m_out.append("─");
				}
			}
			m_out.append("┼\n");

			if (y < b.height) {
				for (int x = 0; x <= b.width; x++) {
					if (b.getForbiddenFlag(x, y, Direction.W)) {
						assert b.getForbiddenFlag(x - 1, y, Direction.E);
						m_out.append("×");
					} else if (b.getExistFlag(x, y, Direction.W)) {
						assert b.getExistFlag(x - 1, y, Direction.E);
						m_out.append("┿");
					} else {
						assert !b.getForbiddenFlag(x - 1, y, Direction.S)
								&& !b.getExistFlag(x - 1, y, Direction.S);
						m_out.append("│");
					}

					if (x < b.width) {
						switch (b.getDangoType(x, y)) {
						case SHIRO:
							m_out.append("○");
							break;
						case KURO:
							m_out.append("●");
							break;
						default:
							m_out.append("・");
						}
					}
				}
				m_out.append("\n");
			}

		}
	}
}
